package com.tanhua.dubbo.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.domain.mongo.VoiceDisLike;
import com.tanhua.domain.mongo.VoiceLike;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 日志消息监听类
 * 将app操作日志消息 写入tb_log表中
 */
@Component
@RocketMQMessageListener(
        topic = "tanhua_voice_dislike_topic",consumerGroup = "my_tanhua_voice_dislike"
)
@Slf4j
public class DislikeMassageListener implements RocketMQListener<String> {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onMessage(String message) {
        //消息数据 将string转为map
        Map<String,Object> map = JSON.parseObject(message, Map.class);
        Integer userId = (Integer) map.get("userId");//userId
        String likeNum = (String) map.get("likeNum");//类型
        //保存数据库
        VoiceDisLike voiceDisLike = new VoiceDisLike();
        voiceDisLike.setId(ObjectId.get());
        voiceDisLike.setCreated(System.currentTimeMillis());
        voiceDisLike.setUserId(userId);
        voiceDisLike.setLikeNum(Integer.parseInt(likeNum));
        mongoTemplate.save(voiceDisLike);
    }
}
